// 2023-03-14 19:18:09
import { GetConnection, GetResult } from "../../util/db.js";
import { JoinSQLInsertKeys, JoinSQLUpdateKeys } from "../../util/JoinSQL.js";
import UserModel from "../../model/userModel/userModel.js";

const result_demo = 'RowDataPacket[] | RowDataPacket[][] | OkPacket | OkPacket[] | ResultSetHeader'


class UserService {
    /**
     * @param {Object} condition 
     */
    static async GetAll(condition = null, onlyIsFreeze = false) {
        try {
            let sql = 'select * from usertable where type = 1 and 1 = 1'
            const conn = await GetConnection();
            if (onlyIsFreeze) {
                sql += '\tand isFreeze = 1\t'
            }
            if (condition) {
                sql += `\tand (userName like '%${condition}%' or email like '%${condition}%') `
            }
            sql += '\torder by isFreeze\t'
            const { rows: result } = await GetResult(sql, condition, conn);
            return result;
        } catch (error) {
            return Promise.reject(error);
        }
    }

    /**
     * @param {UserModel} condition 
     * @returns {Promise<Boolean>}
     */
    static async InsertUser(condition = null) {
        try {
            const model = new UserModel(condition)
            const isexist = await UserService.IsExist(model.phone);
            if (isexist) {
                return Promise.reject(`${model.phone}手机号已经注册!`)
            }
            const connection = await GetConnection();
            let sql = "insert into usertable";
            let { keys, values } = model.GetKeys_Values()
            sql += JoinSQLInsertKeys(keys)
            const { rows: result } = await GetResult(sql, [[values]], connection, true);
            return result && result.affectedRows;
        } catch (error) {
            return Promise.reject(error);
        }
    }

    /**
     * @param {string} phone
     * @param {boolean} tag 标记是否进行管理员判断
     * @example return Boolean
     * @ true:存在 false:不存在
     */
    static async IsExist(phone, tag = false) {
        try {
            const conn = await GetConnection();
            if (!phone || !phone.length) throw '手机号不能为空'
            let sql = "select * from usertable where phone = ?"
            if (tag) {
                sql += "and type = 0"
            }
            const { rows: result } = await GetResult(sql, [phone], conn);
            if (result && result.length) return true;
            return false;
        } catch (error) {
            return Promise.reject(error)
        }
    }

    /**
     * @param {UserModel} condition 
     * @returns {Promise<Boolean>}
     * @introduce true:成功 false:不成功
     */
    static async DeleteUser(condition = null) {
        try {
            const model = new UserModel(condition)
            const phone = model.phone;
            if (!phone || !phone.length) throw '手机号不能为空'
            const conn = await GetConnection();
            let sql = "delete from usertable where phone = ?"
            const { rows: result } = await GetResult(sql, [phone], conn);
            if (result && result.length) return true;
            return false;
        } catch (error) {
            return Promise.reject(error)
        }
    }

    /**
     * @returns {Promise<string>}
     */
    static async GetPortrait(userID) {
        try {
            const conn = await GetConnection()
            let sql = "select portraitSrc from usertable where id = ?"
            const { rows: result } = await GetResult(sql, [userID], conn)
            return result[0]?.portraitSrc
        } catch (error) {
            return Promise.reject(error)
        }
    }
    /**
     * 
     */
    static async GetStar(userID) {
        try {
            const conn = await GetConnection()
            let sql = `
SELECT a.*
FROM usertable a
WHERE a.id != ?`
            const { rows: result } = await GetResult(sql, [[userID]], conn)
            return result
        } catch (error) {

        }
    }

    /**
     * @param {UserModel} model
     * @param {Object} columns
     * @returns {Promise<boolean>}
     */
    static async EditUser(model, columns) {
        try {
            model = new UserModel(model)
            model.SetUpdateColumns(columns)
            let conn = await GetConnection()
            let sql = '\tupdate usertable\t' + JoinSQLUpdateKeys(columns, model)
            let { rows } = await GetResult(sql, undefined, conn, true)
            return rows && rows.affectedRows
        } catch (error) {
            return Promise.reject(error)
        }
    }

    /**
     * @param {string} phone
     */
    static async IsFreeze(phone) {
        try {
            let conn = await GetConnection()
            let sql = 'select * from usertable where phone = ? and isFreeze = 1'
            let { rows } = await GetResult(sql, [phone], conn)
            return rows && rows.length
        } catch (error) {
            return Promise.reject(error)
        }
    }

    /**
     * @param {string} phone
     */
    static async GetUserInfo(phone) {
        try {
            let sql = "select * from usertable where phone = ?"
            let { rows } = await GetResult(sql, [phone], await GetConnection())
            return rows
        } catch (error) {
            return Promise.reject(error)
        }
    }
}

export default UserService;